import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { IProjectManager } from '../model/interfaces'
import { projectManagersApi } from '../api/projectManagersApi'

interface IProjectManagerState {
  projectManagers: IProjectManager[]
}

const initialState: IProjectManagerState = {
  projectManagers: []
}

export const projectManagersSlice = createSlice({
  name: 'projectManagers',
  initialState,
  reducers: {
    setProjectManagers(state, {payload}: PayloadAction<IProjectManager[]>) {
      state.projectManagers = payload
    }
  },
  extraReducers: (builder) => {
    builder.addMatcher(projectManagersApi.endpoints?.getProjectManagers.matchFulfilled, (state, {payload}: PayloadAction<IProjectManager[]>) => {
      state.projectManagers = payload
    })
  }
})

export const projectManagersActions = projectManagersSlice.actions
export const projectManagersReducer = projectManagersSlice.reducer
